package com.iot.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.iot.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iot.system.mapper.DeviceEnergyMapper;
import com.iot.system.domain.DeviceEnergy;
import com.iot.system.service.IDeviceEnergyService;
import org.springframework.util.CollectionUtils;

/**
 * 设备能耗Service业务层处理
 * 
 * @author iot
 * @date 2025-01-02
 */
@Service
@Slf4j
public class DeviceEnergyServiceImpl implements IDeviceEnergyService 
{
    @Autowired
    private DeviceEnergyMapper deviceEnergyMapper;

    /**
     * 查询设备能耗
     * 
     * @param id 设备能耗主键
     * @return 设备能耗
     */
    @Override
    public DeviceEnergy selectDeviceEnergyById(Long id)
    {
        return deviceEnergyMapper.selectDeviceEnergyById(id);
    }

    /**
     * 查询设备能耗列表
     * 
     * @param deviceEnergy 设备能耗
     * @return 设备能耗
     */
    @Override
    public List<DeviceEnergy> selectDeviceEnergyList(DeviceEnergy deviceEnergy)
    {
        return deviceEnergyMapper.selectDeviceEnergyList(deviceEnergy);
    }

    /**
     * 新增设备能耗
     * 
     * @param deviceEnergy 设备能耗
     * @return 结果
     */
    @Override
    public int insertDeviceEnergy(DeviceEnergy deviceEnergy)
    {
        deviceEnergy.setCreateTime(DateUtils.getNowDate());
        return deviceEnergyMapper.insertDeviceEnergy(deviceEnergy);
    }

    /**
     * 修改设备能耗
     * 
     * @param deviceEnergy 设备能耗
     * @return 结果
     */
    @Override
    public int updateDeviceEnergy(DeviceEnergy deviceEnergy)
    {
        return deviceEnergyMapper.updateDeviceEnergy(deviceEnergy);
    }

    /**
     * 批量删除设备能耗
     * 
     * @param ids 需要删除的设备能耗主键
     * @return 结果
     */
    @Override
    public int deleteDeviceEnergyByIds(Long[] ids)
    {
        return deviceEnergyMapper.deleteDeviceEnergyByIds(ids);
    }

    /**
     * 删除设备能耗信息
     * 
     * @param id 设备能耗主键
     * @return 结果
     */
    @Override
    public int deleteDeviceEnergyById(Long id)
    {
        return deviceEnergyMapper.deleteDeviceEnergyById(id);
    }


    /***
     * 获取最新的一条数据
     * @param id
     * @return
     */
    @Override
    public DeviceEnergy getTheLastOne(Long id) {
        DeviceEnergy deviceEnergy = deviceEnergyMapper.getTheLastOne(id);
        return deviceEnergy;
    }

    @Override
    public DeviceEnergy getYesterdayLastOne(Long id, Date date) {
        DeviceEnergy deviceEnergy = deviceEnergyMapper.getYesterdayLastOne(id, date);
        return deviceEnergy;
    }

    @Override
    public String getLineHourEnergy(List<Long> collect, Date startByDate, Date endByDate) {
        log.info("CoreLineHourEnergyInfoServiceImpl_getLineHourEnergy_获取产线小时能耗开始_设备id:{},开始时间:{},结束时间:{}",collect,startByDate,endByDate);
        //判断当前时间是否计算完成
        List<DeviceEnergy> deviceEnergyList = deviceEnergyMapper.getLineHourEnergy(collect,startByDate,endByDate);
        //判断是否为空
        if (CollectionUtils.isEmpty(deviceEnergyList)){
            return "0";
        }
        List<Float> energyAccumulate = new ArrayList<>();
        //分组
        Map<Long, List<DeviceEnergy>> listMap = deviceEnergyList.stream().collect(Collectors.groupingBy(DeviceEnergy::getDeviceId));
        listMap.forEach((k,v)->{
            //根据创建时间排序
            List<DeviceEnergy> deviceEnergies = v.stream().sorted(Comparator.comparing(DeviceEnergy::getCreateTime)).collect(Collectors.toList());
            // 获取第一个和最后一个元素
            DeviceEnergy first = deviceEnergies.get(0);
            DeviceEnergy last = deviceEnergies.get(deviceEnergies.size() - 1);
            //计算能耗差值
            float v1 = last.getDeviceEnergy() - first.getDeviceEnergy();
            energyAccumulate.add(v1);
        });
        Float sum = energyAccumulate.stream()
                .reduce(0.0f, Float::sum);
        log.info("CoreLineHourEnergyInfoServiceImpl_getLineHourEnergy_计算能耗结果为:{}",sum);
        return sum.toString();
    }

    @Override
    public Integer getDeviceEnergyData(Date nowDate) {
        Integer deviceIds = deviceEnergyMapper.getDeviceEnergyData(nowDate);
        if (deviceIds != null){
            return deviceIds;
        }
        return 0;
    }
}
